package com.qiangesoft.flowable.controller;

import com.qiangesoft.flowable.constant.ProcessConstant;
import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 历史 控制器
 *
 * @author qiangesoft
 * @date 2024-04-30
 */
@RestController
@RequestMapping("/history")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @GetMapping("/finished")
    public R finished() {
        // 已结束的流程：按照结束时间倒序
        List<HistoricProcessInstance> processInstanceList = historyService.createHistoricProcessInstanceQuery()
//                .processInstanceId("")
//                .deploymentId("")
//                .processDefinitionName("")
//                .processInstanceBusinessKey("")
                .processDefinitionKey(ProcessConstant.PROCESS_KEY)
                .finished()
                .orderByProcessInstanceEndTime().desc()
                .list();
        return R.ok(processInstanceList.toString());
    }

    @GetMapping("/completed")
    public R completed() {
        // 某人已完成的任务：按照完成时间倒序
        String userId = "zhangsan";
        List<HistoricTaskInstance> processInstanceList = historyService.createHistoricTaskInstanceQuery()
//                .processInstanceId("")
//                .deploymentId("")
//                .processDefinitionName("")
//                .processInstanceBusinessKey("")
                .taskAssignee(userId)
                .processDefinitionKey(ProcessConstant.PROCESS_KEY)
                .finished()
                .orderByHistoricTaskInstanceEndTime().desc()
                .list();
        return R.ok(processInstanceList.toString());
    }

}
